﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCDB.Entity;
using Wf5.Engine.Business;
using Wf5.Engine.Common;


namespace Wf5.Engine.Core
{
    /// <summary>
    /// 结束节点
    /// </summary>
    internal class EndNode : NodeBase, ICompleteAutomaticlly
    {
        internal EndNode(ActivityEntity activity)
            : base(activity)
        {

        }

        #region ICompleteAutomaticlly 成员
        /// <summary>
        /// 自动完成结束节点
        /// </summary>
        /// <param name="processInstance"></param>
        /// <param name="fromActivityInstance"></param>
        /// <param name="activityResource"></param>
        /// <param name="wfLinqDataContext"></param>
        public GatewayExecutedResult CompleteAutomaticlly(WfProcessInstance processInstance,
            Guid transitionGUID,
            WfActivityInstance fromActivityInstance,
            ActivityResource activityResource
            )
        {
            GatewayExecutedResult result = null;
            var toActivityInstance = base.CreateActivityInstanceObject(processInstance, activityResource.LogonUser);

            base.InsertActivityInstance(toActivityInstance  
                 );

            base.CompleteActivityInstance(toActivityInstance.ID,
                activityResource 
                 );

            //写节点转移实例数据
            base.InsertTransitionInstance(processInstance,
                transitionGUID,
                fromActivityInstance,
                toActivityInstance,
                TransitionTypeEnum.Forward,
                TransitionFlyingTypeEnum.NotFlying,
                activityResource.LogonUser 
                 );

            //设置流程完成
            ProcessInstanceManager pim = new ProcessInstanceManager();
            pim.Complete(processInstance.ID, activityResource.LogonUser);

            return result;
        }

        #endregion
    }
}
